namespace CIS.Resource
{
    using CISLib;
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class ResourceTipPageEdit : Page
    {
        protected Image iResource;
        protected Label lbTipsContent;
        protected Label lbTipsSubject;
        protected UserPrincipal Principal = null;
        protected Button SaveBtn;
        protected TextBox TipsContentTxb;
        protected TextBox TipsSubjectTxb;

        private void FillPage()
        {
            if (base.Request.QueryString["IsModify"] == "True")
            {
                Guid id = new Guid(base.Request.QueryString["TipsId"]);
                ResourceTip resourceTip = ManageResource.GetResourceTipInfoViaId(id);
                this.TipsSubjectTxb.Text = resourceTip.TipsSubject;
                this.TipsContentTxb.Text = resourceTip.TipsContent;
            }
        }

        private void InitializeComponent()
        {
            this.SaveBtn.Click += new EventHandler(this.SaveBtn_Click);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            this.SetFromImage();
            this.SetFrom();
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.SelectAuth();
            }
        }

        private void SaveBtn_Click(object sender, EventArgs e)
        {
            string strTipsSubject = this.TipsSubjectTxb.Text.Trim();
            string strTipsContent = this.TipsContentTxb.Text;
            if (base.Request.QueryString["IsModify"] == "True")
            {
                if (strTipsSubject != "")
                {
                    ManageResource.UpdateResourceTipInfo(new Guid(base.Request.QueryString["TipsId"]), strTipsSubject, strTipsContent);
                    base.Response.Redirect("~/CIS/Resource/ResourceTipPage.aspx", true);
                }
                base.Response.Write("<script> alert('Name is Null !') </script>");
            }
            else if (strTipsSubject != "")
            {
                if (!ManageResource.IsExistSameResourceTipInfo(strTipsSubject))
                {
                    ManageResource.InsertNewResourceTipInfo(strTipsSubject, strTipsContent);
                    base.Response.Redirect("~/CIS/Resource/ResourceTipPage.aspx", true);
                }
                else
                {
                    base.Response.Write("<script> alert('该信息已存在') </script>");
                }
            }
        }

        private void SelectAuth()
        {
            if (this.Principal.OwnRole.TypeD == 0)
            {
                this.InitPage();
                this.FillPage();
                this.SaveBtn.Visible = false;
            }
            else if (this.Principal.OwnRole.TypeD == 1)
            {
                this.InitPage();
                this.FillPage();
            }
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/CIS/LanguageConfig.xml");
            this.lbTipsSubject.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceTipPageEdit", "lbTipsSubject", language, path);
            this.lbTipsContent.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceTipPageEdit", "lbTipsContent", language, path);
            this.SaveBtn.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceTipPageEdit", "SaveBtn", language, path);
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iResource.ImageUrl = "../images/title_SharedInfo.jpg";
                    break;

                case "en":
                    this.iResource.ImageUrl = "../images/title_SharedInfoEn.jpg";
                    break;
            }
        }
    }
}

